package com.ruoyi.service.impl;

import com.ruoyi.service.TxyService;
import com.ruoyi.utils.LiveUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class TxyServiceImpl implements TxyService {
    @Value("${tenxunyun.live.playDomain}")
    private String playDomain;
    @Value("${tenxunyun.live.pushDomain}")
    private String pushDomain;
    @Value("${tenxunyun.live.pushKey}")
    private String pushKey;
    @Value("${tenxunyun.live.appName}")
    private String appName;
    @Value("${tenxunyun.live.defaultType}")
    private String defaultType;

    /**
     * 获取推流地址, 默认过期时间为一天
     * @param streamName 推流的名称
     * @return {@code String}
     */
    @Override
    public String getPushAddress(String streamName) {
        Date date = new Date(System.currentTimeMillis() + 24*60*60*1000);
        String pushAddress = defaultType + pushDomain + "/" + appName + "/" +
                streamName + "?";
        String safeUrl = LiveUtils.getSafeUrl(pushKey, streamName, date.getTime()/1000);
        pushAddress += safeUrl;
        return pushAddress;
    }

    @Override
    public String getPushAddress(String type, String streamName) {
        Date date = new Date(System.currentTimeMillis() + 24*60*60*1000);
        String pushAddress = type + pushDomain + "/" + appName + "/" +
                streamName + "?";
        String safeUrl = LiveUtils.getSafeUrl(pushKey, streamName, date.getTime()/1000);
        pushAddress += safeUrl;
        return pushAddress;
    }

    /**
     * @param streamName 推流地址
     * @return {@code String}
     */
    @Override
    public String getPlayAddress(String streamName) {
        String playAddress = defaultType + playDomain + "/" + appName + "/" +
                streamName;
        return playAddress;
    }

    @Override
    public String getPlayAddress(String type, String streamName) {
        String playAddress = type + playDomain + "/" + appName + "/" +
                streamName;
        return playAddress;
    }
}
